﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class UdfReportSetting : BasePage
    {
        protected Button btnAddDetail;
        protected Button btnDelete;
        protected Button btnDeleteDetail;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected HtmlForm F1871;
        protected GridView gvUdfReportSettingDetail;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnReportFile;
        protected HtmlInputHidden hdnReportID;
        protected HtmlInputHidden hdnSeqNo;
        protected Label lblDataSourceName1;
        protected Label lblDataSourceName2;
        protected Label lblMsg;
        protected Label lblRemark;
        protected Label lblReportID;
        protected Label lblReportName;
        protected Label lblReprotFile;
        protected Label lblSQL;
        protected Label lblUdfReportSetting;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected TextBox txtDataSourceName1;
        protected TextBox txtDataSourceName2;
        protected TextBox txtRemark;
        protected TextBox txtReportFile;
        protected TextBox txtReportID;
        protected TextBox txtReportName;
        protected TextBox txtSQL;

        protected void btnAddDetail_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtReportName.Text.Trim()))
            {
                this.lblMsg.Text = "报表名称不能为空！";
            }
            else
            {
                if (string.IsNullOrEmpty(this.hdnReportID.Value))
                {
                    this.InsertUdfReportSettingInfo();
                    this.hdnSeqNo.Value = "10";
                }
                base.Response.Redirect(string.Format("UdfReportSettingDetail.aspx?from={0}&ID={1}&SeqNO={2}", base.UrlEncode(this.hdnFrom.Value), base.UrlEncode(this.txtReportID.Text.Trim()), base.UrlEncode(this.hdnSeqNo.Value)), true);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtReportID.Text.Trim()))
            {
                try
                {
                    List<string> udfReportSettingInfo = new List<string> {
                        this.txtReportID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(udfReportSettingInfo, "UDF_REPORT_SETTING") + base.GetGlobalMsgResource("F1800_002");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F1800_003") + ex.Message;
                }
            }
        }

        protected void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtReportID.Text.Trim()) && (this.hdnFlag.Value == "1"))
            {
                List<string> reportID = base.GetSelectedIDsInGridView(this.gvUdfReportSettingDetail, "ReportID");
                List<string> LineID = base.GetSelectedIDsInGridView(this.gvUdfReportSettingDetail, "LineID");
                try
                {
                    this.lblMsg.Text = BaseInfoDetailServiceClientProxy.GetInstance().DeleteDetail(reportID, LineID, new List<string>(), "UDF_REPORT_SETTING_DETAIL") + base.GetGlobalMsgResource("C006");
                    this.SetUdfReportSettingInfo2Form();
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F1870_002") + ex.Message;
                }
            }
            else
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1870_003");
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("UdfReportSettingQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("UdfReportSettingList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtReportName.Text.Trim()))
            {
                this.lblMsg.Text = "报表名称不能为空！";
            }
            else if (this.hdnFlag.Value == "1")
            {
                this.UpdateUdfReportSettingInfo();
            }
            else
            {
                this.InsertUdfReportSettingInfo();
            }
        }

        protected void gvUdfReportSettingDetail_Init(object sender, EventArgs e)
        {
        }

        protected void gvUdfReportSettingDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as UdfReportSettingInfo).UdfReportSettingDetailInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
        }

        protected void gvUdfReportSettingDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvUdfReportSettingDetail.PageIndex * this.gvUdfReportSettingDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
            }
        }

        protected void gvUdfReportSettingDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                UdfReportSettingInfo udfReportSettingInfo = base.DataEntityViewState as UdfReportSettingInfo;
                DataView dv = CADataConverter.ToDataTable(udfReportSettingInfo.UdfReportSettingDetailInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvUdfReportSettingDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        private void InsertUdfReportSettingInfo()
        {
            try
            {
                UdfReportSettingInfo UdfReportSettingInfo = new UdfReportSettingInfo {
                    ReportID = this.txtReportID.Text,
                    ReportName = this.txtReportName.Text,
                    ReportFile = this.txtReportFile.Text,
                    Remark = this.txtRemark.Text,
                    DataSourceName1 = this.txtDataSourceName1.Text,
                    DataSourceName2 = this.txtDataSourceName2.Text,
                    DataSql = this.txtSQL.Text.Trim()
                };
                string strReprotID = BaseInfoServiceClientProxy.GetInstance().Insert(UdfReportSettingInfo, "UDF_REPORT_SETTING");
                this.txtReportID.Text = strReprotID;
                this.lblMsg.Text = strReprotID + "保存成功!";
                base.DataEntityViewState = UdfReportSettingInfo;
                this.hdnFlag.Value = "1";
                this.txtReportID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1800_008") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnReportID.Value = base.Request.QueryString["ID"];
                this.SetUdfReportSettingInfo2Form();
                this.hdnFlag.Value = "1";
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"];
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetControlsUdfReportSettingInfoText(UdfReportSettingInfo udfReportSettingInfo)
        {
            if (udfReportSettingInfo != null)
            {
                this.txtReportID.Text = udfReportSettingInfo.ReportID;
                this.txtReportID.Enabled = false;
                this.txtReportName.Text = udfReportSettingInfo.ReportName;
                this.txtReportFile.Text = udfReportSettingInfo.ReportFile;
                this.txtRemark.Text = udfReportSettingInfo.Remark;
                this.txtDataSourceName1.Text = udfReportSettingInfo.DataSourceName1;
                this.txtDataSourceName2.Text = udfReportSettingInfo.DataSourceName2;
                this.txtSQL.Text = udfReportSettingInfo.DataSql;
            }
        }

        private void SetUdfReportSettingInfo2Form()
        {
            try
            {
                string StrReportID = base.Request.QueryString["ID"].ToString();
                UdfReportSettingInfo udfReportSettingInfo = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(StrReportID, "UDF_REPORT_SETTING") as UdfReportSettingInfo;
                base.DataEntityViewState = udfReportSettingInfo;
                this.SetControlsUdfReportSettingInfoText(udfReportSettingInfo);
                if ((udfReportSettingInfo.UdfReportSettingDetailInfos != null) && (udfReportSettingInfo.UdfReportSettingDetailInfos.Count > 0))
                {
                    this.hdnSeqNo.Value = (udfReportSettingInfo.UdfReportSettingDetailInfos[udfReportSettingInfo.UdfReportSettingDetailInfos.Count - 1].LineID + 10).ToString();
                }
                else
                {
                    this.hdnSeqNo.Value = "10";
                }
                base.BindData2GridView(this.gvUdfReportSettingDetail, udfReportSettingInfo.UdfReportSettingDetailInfos);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void SetUdfReportSettingInfo3Form()
        {
            string strReprotID = string.Empty;
            if (!string.IsNullOrEmpty(this.txtReportID.Text))
            {
                strReprotID = this.txtReportID.Text;
            }
            else
            {
                strReprotID = base.Request.QueryString["ID"];
            }
            UdfReportSettingInfo udfReportSettingInfo = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(strReprotID, "UDF_REPORT_SETTING") as UdfReportSettingInfo;
            base.DataEntityViewState = udfReportSettingInfo;
            this.SetControlsUdfReportSettingInfoText(udfReportSettingInfo);
        }

        private void UpdateUdfReportSettingInfo()
        {
            try
            {
                UdfReportSettingInfo UdfReportSettingInfo = base.DataEntityViewState as UdfReportSettingInfo;
                UdfReportSettingInfo.ReportName = this.txtReportName.Text;
                UdfReportSettingInfo.ReportFile = this.txtReportFile.Text;
                UdfReportSettingInfo.Remark = this.txtRemark.Text;
                UdfReportSettingInfo.DataSourceName1 = this.txtDataSourceName1.Text;
                UdfReportSettingInfo.DataSourceName2 = this.txtDataSourceName2.Text;
                UdfReportSettingInfo.DataSql = this.txtSQL.Text.Trim();
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(UdfReportSettingInfo, "UDF_REPORT_SETTING") + base.GetGlobalMsgResource("F1800_009");
                base.DataEntityViewState = UdfReportSettingInfo;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1800_010") + ex.Message;
            }
        }
    }
}

